package view;

import dao.DataManager;
import model.User;
import model.UserType;
import util.InputUtil;

public class LoginView {
    private final DataManager dataManager;

    public LoginView() {
        this.dataManager = DataManager.getInstance();
    }

    public User login() {
        System.out.println("\n===== 影院管理系统登录 =====");
        String username = InputUtil.readNonEmptyString("请输入用户名: ");
        String password = InputUtil.readNonEmptyString("请输入密码: ");
        
        User user = dataManager.getUserByUsername(username);
        
        if (user == null || !user.getPassword().equals(password)) {
            System.out.println("用户名或密码错误，请重试！");
            return null;
        }
        
        System.out.println("登录成功，欢迎您 " + username + "！");
        
        // 检查是否需要重置密码
        if (user.isNeedResetPassword()) {
            System.out.println("您的密码已被重置，需要修改新密码");
            changePassword(user);
        }
        
        return user;
    }
    
    private void changePassword(User user) {
        while (true) {
            String newPassword = InputUtil.readNonEmptyString("请输入新密码: ");
            String confirmPassword = InputUtil.readNonEmptyString("请再次输入新密码: ");
            
            if (!newPassword.equals(confirmPassword)) {
                System.out.println("两次输入的密码不一致，请重新输入！");
                continue;
            }
            
            user.setPassword(newPassword);
            user.setNeedResetPassword(false);
            dataManager.updateUser(user);
            
            System.out.println("密码修改成功！");
            break;
        }
    }
} 